package momo.multitree.simulation.result;

import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import momo.multitree.structure.Graph;
import momo.multitree.structure.Tree;
import momo.multitree.simulation.SimulatedAnnealing;

public class SettingsComparisonSimulation {
	
	private Log log = LogFactory.getLog(SettingsComparisonSimulation.class);

//	private int[] iteration = 	{100,	100,	100,	100,	100,	100,	100,	100		};
//	private int[] round = 		{100,	100,	100,	100,	1000,	1000,	1000,	1000	};
//	private double[] temp =		{0.0,	1,		0.1,	0.01,	0.0,	1,		0.1,	0.01 	};
//	private double[] factor = 	{0,		0.95,	0.97,	0.99,	0,		0.95,	0.97,	0.99	};
	
	private int[] iteration = 	{100,	100,	100,	100	};
	private int[] round = 		{100,	100,	100,	100 };
	private double[] temp =		{0.0,	1,		0.1,	0.01 };
	private double[] factor = 	{0,		0.95,	0.97,	0.99 };
	
	private int sizes[] = {50};
	private String[] filenames;
	
	public SettingsComparisonSimulation() {
		filenames = new String[ sizes.length*100 ];
		int counter = 0;
		for(int i=0; i<sizes.length; i++) {
			for(int j=0; j<100; j++) {
				filenames[ j + (counter*100) ] = sizes[i]+"_node_symmetric_noleaf_dataset_" + (j+1) +".txt";
//				filenames[ j + (counter*100) ] = sizes[i]+"_node_symmetric_dataset_" + (j+1) +".txt";
			}
			counter ++;
		}
	}
	
	private Map<String, Vector<String>> getResults() {
		
		Map<String, Vector<String>> map = new LinkedHashMap<String, Vector<String>>();
		
		for(int i=0; i<iteration.length; i++) {
			int iteration = this.iteration[i];
			int round = this.round[i];
			double temp = this.temp[i];
			double factor = this.factor[i];
						
			for(String filename : filenames) {
				try {
					Graph g = new Graph(new FileReader("dataset/"+filename));
					SimulatedAnnealing sim = new SimulatedAnnealing(g, iteration, round, temp, factor);
					
					Vector<String> val = map.get(filename);
					if ( val == null )
						val = new Vector<String>();
					map.put(filename, val);
					Tree optTree = sim.optimizeTree();
					double dist = sim.score(optTree);
					log.info(filename +", " + dist);
					val.add( ""+dist );
					
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		return map;
	}
	
	private String toCSVFormat() {
		StringBuffer buffer = new StringBuffer("filename");
		for(int i=0; i<iteration.length; i++) {
			int iteration = this.iteration[i];
			int round = this.round[i];
			double temp = this.temp[i];
			double factor = this.factor[i];
			
			String settings = ",i_" + iteration+"_r_" + round + "_t_" + temp  + "_f_" + factor;
			buffer.append(settings);
		}
		
		Map<String, Vector<String>> result = this.getResults();
		for ( String key : result.keySet() ) {
			buffer.append("\n"+key);
			for (String data : result.get(key)) {
				buffer.append(","+data);
			}
		}
		return buffer.toString();
	}
	
	private void writeToFile(String filename, String result) {
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(filename));
			out.write(result);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void getResult(String filename) {
		String csvFormat = this.toCSVFormat();
		this.writeToFile(filename, csvFormat);
	}
	
	public static void main (String args[]) {
		SettingsComparisonSimulation app = new SettingsComparisonSimulation();
		app.getResult("50__newSwap_no_leaves.csv");
	}
	
}//end of class SettingsComparisonSimulation
